//
//  BCProjectDetailsHeaderView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/22.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCProjectDetailsHeaderView.h"

#import "BCProjectDetailBaseCell.h"


#import <OCSkeleton.h>

@interface BCProjectDetailsHeaderView () <OCGradientsOwner>

@property (nonatomic, strong) OCGradientContainerView *placeholderContainerView;

@end

@implementation BCProjectDetailsHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        [self setupSubviews];
        [self setContainerViews];
    }
    return self;
}

- (void)setupSubviews {
    self.backButton = [QMUIButton new];
    [self.contentView addSubview:self.backButton];
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.contentView);
    }];
    [self.backButton addTarget:self action:@selector(backButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
    
    //
    self.titleLabel = [UILabel new];
    self.titleLabel.textColor = [UIColor qmui_colorWithHexString:@"#333333"];
    self.titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
    [self.backButton addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backButton);
        make.left.mas_equalTo(self.backButton).offset(BCProjectDetailCellLeftPadding);
    }];
    self.detailsLabel = [UILabel new];
    self.detailsLabel.textColor = [UIColor qmui_colorWithHexString:@"#AEAEAE"];
    self.detailsLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    self.detailsLabel.textAlignment = NSTextAlignmentRight;
    [self.backButton addSubview:self.detailsLabel];
    //
    self.rightRightArrow = [UIImageView new];
//    self.rightRightArrow.image = arrowImage;
    [self.backButton addSubview:self.rightRightArrow];
    [self.rightRightArrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.backButton).offset(-BCProjectDetailCellRightPadding);
        make.centerY.mas_equalTo(self.backButton);
//        make.size.mas_equalTo(CGSizeMake(20.0, 20.0));
    }];
    
    [self.detailsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.rightRightArrow.mas_left).offset(-10.0);
        make.centerY.mas_equalTo(self.backButton);
        make.left.mas_equalTo(self.titleLabel.mas_right).offset(5.0);
    }];
    
    
    //
    self.detailButton = [QMUIButton new];
    [self.contentView addSubview:self.detailButton];
    [self.detailButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backButton);
        make.left.mas_equalTo(self.titleLabel.mas_right).offset(7.0);
    }];
    [self.detailButton addTarget:self action:@selector(detailButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setContainerViews {
    self.placeholderContainerView = [OCGradientContainerView new];
    [self.contentView addSubview:self.placeholderContainerView];
    [self.placeholderContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel);
        make.centerY.mas_equalTo(self.titleLabel);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(self.titleLabel.font.lineHeight-5.0);
    }];
}


- (void)setSectionData:(BCProjectDetailSectionModel *)sectionData {
    _sectionData = sectionData;
    //
    if (sectionData.isHandleData) {
        self.placeholderContainerView.alpha = 0;
        self.placeholderContainerView.backgroundColor = [UIColor clearColor];
        //
        self.titleLabel.text = sectionData.sectionTitle;
        self.detailsLabel.text = sectionData.sectionDetails;
        //
        if (sectionData.detailButtonImageName.length) {
            [self.detailButton setImage:LOAD_IMAGE(sectionData.detailButtonImageName) forState:UIControlStateNormal];
        }else {
            [self.detailButton setImage:nil forState:UIControlStateNormal];
        }
        //
        if (sectionData.needRightArrow) {
            UIImage *arrowImage = LOAD_IMAGE(@"menu_detail_arrow");
            self.rightRightArrow.image = arrowImage;
            [self.rightRightArrow mas_updateConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(arrowImage.size);
            }];
        }else {
            self.rightRightArrow.image = nil;
            [self.rightRightArrow mas_updateConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeZero);
            }];
        }
        //
        self.backButton.userInteractionEnabled = sectionData.canClick;
    }else {
        self.titleLabel.text = nil;
        self.rightRightArrow.image = nil;
        self.backButton.userInteractionEnabled = false;
        [self.detailButton setImage:nil forState:UIControlStateNormal];
        self.detailsLabel.text = nil;
        //
        self.placeholderContainerView.alpha = 1;
        self.placeholderContainerView.backgroundColor = [OCGradientContainerView appearance].backgroundColor;
    }
}


#pragma mark - OCGradientsOwner
- (NSArray <OCGradientLayer *>*)gradientLayers {
    if (self.placeholderContainerView) {
        return @[self.placeholderContainerView.gradientLayer];
    }
    return nil;
}

- (NSArray <UIView *>*)skeletonViews {
    if (self.placeholderContainerView) {
        return @[self.placeholderContainerView];
    }
    return nil;
}


#pragma mark - action
//detail click
- (void)detailButtonDidClickedAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(projectDetailsHeaderView:didClickedDetailButtonWithSectionModel:detailButtonConvertFrame:)]) {
        [self.delegate projectDetailsHeaderView:self didClickedDetailButtonWithSectionModel:self.sectionData detailButtonConvertFrame:self.detailButton.frame];
    }
}

#pragma mark - backButtonDidClickedAction
- (void)backButtonDidClickedAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(projectDetailsHeaderView:didClickedBackButtonWithSectionModel:)]) {
        [self.delegate projectDetailsHeaderView:self didClickedBackButtonWithSectionModel:self.sectionData];
    }
}



+ (CGFloat)defaultHeight {
    return 40.0f;
}

@end
